package no.uib.hplims.models;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.*;

import org.vaadin.appfoundation.persistence.data.AbstractPojo;


@Entity
public class MyOrder extends AbstractPojo implements Serializable {

	private static final long serialVersionUID = -8221535097096508600L;

	private String orderTitle = null;
	
	private String salesOrderCode = null;

	private boolean isFinalized;

	@Temporal(TemporalType.DATE)
	private Date createdOn;

	@ManyToOne
	private User owner;
	
	private List<Long> peptideIDs = new ArrayList<Long>();

	public MyOrder() {
		salesOrderCode = "N/A";
		isFinalized = false;
	}

	/**
	 * @return the serialversionuid
	 */
	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	/**
	 * @return the orderTitle
	 */
	public String getOrderTitle() {
		return orderTitle;
	}

	/**
	 * @param orderTitle the orderTitle to set
	 */
	public void setOrderTitle(String orderTitle) {
		this.orderTitle = orderTitle;
	}

	public boolean isFinalized() {
		return isFinalized;
	}

	public void setFinalized(boolean isFinalized) {
		this.isFinalized = isFinalized;
	}

	/**
	 * @return the createdOn
	 */
	public Date getCreatedOn() {
		return createdOn;
	}

	/**
	 * @param createdOn the createdOn to set
	 */
	public void setCreatedOn(Date createdOn) {
		this.createdOn = createdOn;
	}

	/**
	 * @return the owner
	 */
	public User getOwner() {
		return owner;
	}

	/**
	 * @param owner the owner to set
	 */
	public void setOwner(User owner) {
		this.owner = owner;
	}

	public int size() {
		return getPeptideIDs().size();
	}

	public String getSalesOrderCode() {
		return salesOrderCode;
	}

	public void setSalesOrderCode(String salesOrderCode) {
		this.salesOrderCode = salesOrderCode;
	}

	public List<Long> getPeptideIDs() {
		return peptideIDs;
	}

	public void setPeptideIDs(List<Long> peptideIDs) {
		this.peptideIDs = peptideIDs;
	}
	
	public void addPeptide(Long id) {
		this.peptideIDs.add(id);
	}
	
	public void addPeptide(Peptide p) {
		this.peptideIDs.add(p.getId());
	}
	
	public void removePeptide(Long id) {
		this.peptideIDs.remove(id);
	}
	
	public void removePeptide(Peptide p) {
		this.peptideIDs.remove(p.getId());
	}

	public String toString() {
		return this.orderTitle;
	}

	@Override
	public boolean equals(Object that) {
		if (that == null) return false;
		if (this == that) return true;
		if (!(that instanceof MyOrder)) return false;
		
		MyOrder thatOrder = (MyOrder) that;
		
		if (this.getId().equals(thatOrder.getId())) {
			return true;
		}
		
		return false;
	}
}
